using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Update.Controls
{
    public class ModernTextBox : RichTextBox
    {
        #region 字段

        // 滚动条颜色
        private Color _thumbColor = Color.FromArgb(104, 104, 104);      // 滚动条滑块颜色
        private Color _thumbHoverColor = Color.FromArgb(158, 158, 158); // 鼠标悬停时滚动条滑块颜色
        private Color _thumbPressedColor = Color.FromArgb(0, 122, 204); // 鼠标按下时滚动条滑块颜色
        private Color _trackColor = Color.FromArgb(45, 45, 48);         // 滚动条轨道颜色
        private int _scrollBarWidth = 8;                                // 滚动条宽度

        #endregion

        #region 属性

        [Category("Appearance")]
        public Color ThumbColor
        {
            get => _thumbColor;
            set
            {
                _thumbColor = value;
                SetScrollBarThemeInfo();
            }
        }

        [Category("Appearance")]
        public Color ThumbHoverColor
        {
            get => _thumbHoverColor;
            set
            {
                _thumbHoverColor = value;
                SetScrollBarThemeInfo();
            }
        }

        [Category("Appearance")]
        public Color ThumbPressedColor
        {
            get => _thumbPressedColor;
            set
            {
                _thumbPressedColor = value;
                SetScrollBarThemeInfo();
            }
        }

        [Category("Appearance")]
        public Color TrackColor
        {
            get => _trackColor;
            set
            {
                _trackColor = value;
                SetScrollBarThemeInfo();
            }
        }

        [Category("Appearance")]
        public int ScrollBarWidth
        {
            get => _scrollBarWidth;
            set
            {
                _scrollBarWidth = value;
                SetScrollBarThemeInfo();
            }
        }

        #endregion

        #region 构造函数

        public ModernTextBox()
        {
            // 设置双缓冲以减少闪烁
            SetStyle(ControlStyles.OptimizedDoubleBuffer | 
                     ControlStyles.AllPaintingInWmPaint | 
                     ControlStyles.ResizeRedraw, true);

            // 初始化滚动条
            InitializeScrollBarTheme();
        }

        #endregion

        #region 方法

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            
            // 设置滚动条主题
            SetScrollBarThemeInfo();
        }

        private void InitializeScrollBarTheme()
        {
            // 初始化滚动条主题
            if (IsHandleCreated)
            {
                SetScrollBarThemeInfo();
            }
        }

        private void SetScrollBarThemeInfo()
        {
            if (!IsHandleCreated)
                return;

            // 设置滚动条为系统主题
            SetWindowTheme(Handle, "Explorer", null);

            // 设置滚动条颜色
            SCROLLBARINFO scrollInfo = new SCROLLBARINFO();
            scrollInfo.cbSize = Marshal.SizeOf(scrollInfo);

            // 设置滚动条宽度
            int elementSize = _scrollBarWidth;
            bool result = SystemParametersInfo(SPI_SETSCROLLWIDTH, (uint)elementSize, ref elementSize, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

            // 应用滚动条颜色
            SCROLLBARCOLORS colors = new SCROLLBARCOLORS();
            colors.thumbColor = ColorTranslator.ToWin32(_thumbColor);
            colors.thumbHoverColor = ColorTranslator.ToWin32(_thumbHoverColor);
            colors.thumbPressedColor = ColorTranslator.ToWin32(_thumbPressedColor);
            colors.trackColor = ColorTranslator.ToWin32(_trackColor);

            IntPtr colorsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(colors));
            Marshal.StructureToPtr(colors, colorsPtr, false);

            // 发送消息设置滚动条颜色
            SendMessage(Handle, WM_SETSCROLLBARCOLORS, IntPtr.Zero, colorsPtr);

            Marshal.FreeHGlobal(colorsPtr);
        }

        #endregion

        #region Windows API

        // Windows API 常量
        private const int WM_VSCROLL = 0x115;
        private const int WM_HSCROLL = 0x114;
        private const int WM_SETSCROLLBARCOLORS = 0x0409; // 自定义消息
        private const int SPI_SETSCROLLWIDTH = 0x000A;
        private const int SPIF_UPDATEINIFILE = 0x01;
        private const int SPIF_SENDCHANGE = 0x02;

        [StructLayout(LayoutKind.Sequential)]
        private struct SCROLLBARINFO
        {
            public int cbSize;
            public Rectangle rcScrollBar;
            public int dxyLineButton;
            public int xyThumbTop;
            public int xyThumbBottom;
            public int reserved;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public int[] rgstate;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SCROLLBARCOLORS
        {
            public int thumbColor;
            public int thumbHoverColor;
            public int thumbPressedColor;
            public int trackColor;
        }

        [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
        private static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);

        #endregion
    }
} 